package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Result;
import com.example.dao.StudentInfoDao;
import com.example.entity.*;
import com.example.exception.CustomException;
import com.example.service.ClassInfoService;
import com.example.service.XuankeInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.genid.GenId;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/classInfo")
public class ClassInfoController {
    @Resource
    ClassInfoService classInfoService;

    @Resource
    XuankeInfoService xuankeInfoService;
    @Autowired
    private StudentInfoDao studentInfoDao;

    @PostMapping
    public Result add(@RequestBody ClassInfo classInfo){
        classInfoService.add(classInfo);
        return Result.success();
    }

    @GetMapping
    public Result findAll(){
        List<ClassInfo> list = classInfoService.findAll();
        return Result.success(list);
    }
    @PutMapping
    public Result update(@RequestBody ClassInfo classInfo){
        classInfoService.update(classInfo);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable(value = "id") Long id){
        classInfoService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageInfo<ClassInfo> info = classInfoService.findPage(pageNum,pageSize);
        return Result.success(info);
    }

    @GetMapping("/page/{name}")
    public Result findPageName(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @PathVariable String name){
        PageInfo<ClassInfo> info = classInfoService.findPageName(pageNum,pageSize,name);
        return Result.success(info);
    }

    /**
     * 选课接口
     * @param classInfo
     * @param request
     * @return
     */
    @PostMapping("/xuanke")
    public Result xuanke(@RequestBody ClassInfo classInfo, HttpServletRequest request){
        //TODO 业务完善
        //1.学生才可以选课（level=3）
        //2.对应专业年级专业的学生开发选课
        //3.不能重复选课
        //4.1获取锁(studentId)
        //4.2
        //

        Account user=(Account) request.getSession().getAttribute("user");
        if(ObjectUtil.isEmpty(user)){
            throw new CustomException("-1","登录失效，请重新登录");
        }
        XuankeInfo xuankeInfo = new XuankeInfo();


        if(3 == user.getLevel()) {

            //已选人数不能多于开班人数
            if (classInfo.getKaiban().equals(classInfo.getYixuan())) {
                xuankeInfo.setStatus("已招满");
                throw new CustomException("-1", "该课程人数已满");

            }
            XuankeInfo info = xuankeInfoService.find(classInfo.getName(), classInfo.getTeacherid(), user.getId());
            //判断是否开课
//            if(info.getStatus().equals("已开课"))
//            {
//                throw new CustomException("-1", "该课程已经开课了，不能再选该课程");
//            }
            //判断是否已经选过课了，对于已经选过的课不能再选了

            if (ObjectUtil.isNotEmpty(info)) {
                throw new CustomException("-1", "你已经选过这门课了");
            }

            //拷贝课程信息
            BeanUtils.copyProperties(classInfo, xuankeInfo);
            xuankeInfo.setId(null);//id不要
            //把选课信息表剩下的字段补全
            xuankeInfo.setStudentid(user.getId());
            xuankeInfo.setStatus("待开课");
            xuankeInfoService.add(xuankeInfo);
        }
        //已选人数加一
        //if(2 == user.getLevel()) {
        //    classInfo.setYixuan(classInfo.getYixuan() + 1);
        //}
        classInfo.setYixuan(classInfo.getYixuan() + 1);
        classInfoService.update(classInfo);
        //选课表也加一
        xuankeInfoService.update(xuankeInfo);
        //改成确认录取后聘用人数加一
        return Result.success();
    }
}
